<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------


/**
 * 资源位ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   allen.qiang
 */
define('ARTICE_NOTICE_TYPE',10);

quanli_require('app_class/contentHandler.class.php');

class ajax_article {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询文章分类信息
                    $this -> searchArticleCategoryList();
                    break;
                case 2: //保存或编辑文章分类信息
                    $this -> saveArticleCategory();
                    break;   
               case 6: //删除文章分类信息
                    $this->deleteArticleCategory();
                    break;
               case 3: //查询文章信息
                    $this -> searchArticleList();
                    break;
               case 4: //保存文章
               case 5: //编辑文章
               		$this -> saveArticle();
               		break;
               case 7:
               		$this->deleteArticle();
               		break;
                case 8:  //查询文章评论列表
                    $this->articleCommentList();
                    break;
                case 9:  //保存文章回复内容
                    $this->articleReply();
                    break;
                case 10:  //删除文章评论
                    $this->delArticleComment();
                    break;

            }
			
        }

    }
    
	/**
     * 删除文章分类信息
     */
    private function deleteArticleCategory() {

        global $G_PGLOB;

        $contentHandler = new contentHandler( $G_PGLOB->DBCA );

        $result_array = $contentHandler->deleteArticleCategory( $_REQUEST );

        echo json_encode($result_array);

    }
    /**
     * 删除文章信息
     *
     */
	private function deleteArticle(){
		global $G_PGLOB;
		
		$contentHandler = new contentHandler( $G_PGLOB->DBCA );

        $result_array = $contentHandler->deleteArticle( $_REQUEST );

        echo json_encode($result_array);
		
	}


    /**
     * 查询文章分类信息
     */
    private function searchArticleCategoryList() {

        global $G_PGLOB;

        //实例化内容类
        $contentHandler = new contentHandler( $G_PGLOB->DBCA );

        //查询文章分类
        $category_array = $contentHandler->searchArticleCategory( $_REQUEST );


        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($category_array) {
            foreach($category_array as &$cate) {
                $cate_id = &$cate['category_id'];
                if ($cate['category_type'] == 1)
                    $category_type_name = '帮助文章';
                else if ($cate['category_type'] == 2)
                    $category_type_name = '公告文章';
                else if ($cate['category_type'] == 3)
                    $category_type_name = '新闻文章';
                else
                    $category_type_name = '';
                $result_array['rows'][] = array(
                    'id' => $cate_id,
                    'cell' => array(
                        '<a href="javascript: void(0);" onclick="article.edit(' . $cate_id . ');">修改</a>&nbsp;',
                        $cate['category_id'],
                        '&nbsp;&nbsp;&nbsp;&nbsp;' . $cate['category_title'],
                        $cate['category_index'],
                        $category_type_name,
                        $cate['description'],
                        $cate['useFlg'] == 1 ? '使用' : '禁用'
                    )
                );
            }
        }

        echo json_encode($result_array);

    }
    
    /**
     * 保存或编辑文章分类信息
     */
    private function saveArticleCategory() {

        global $G_PGLOB;
        
        $contentHandler = new contentHandler( $G_PGLOB->DBCA );

        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
		
        $result_array = $contentHandler->addArticleCategory( $_REQUEST );

        echo json_encode($result_array);

    }
    
    /**
     * 查询文章信息
     */
    private function searchArticleList() {

        global $G_PGLOB;

        //实例文章类
        $contentHandler = new contentHandler( $G_PGLOB->DBCA );

        //查询文章信息
        $article_array = $contentHandler->searchArticle( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($article_array) {
            foreach($article_array as &$article) {
                $article_id = $article['article_id'];

                $preview_url = '/shop/article-' . $article_id . '.html?' . echo_preview_url();
                $publish_url = '/shop/article-' . $article_id . '.html?' . echo_publish_url();

                $result_array['rows'][] = array(
                    'id' => $article_id,
                    'cell' => array(
                        '<a href="javascript: void(0);" onclick="article.edit(' . $article_id . ')">修改</a>' .
                        '&nbsp;&nbsp;<a href="' . $preview_url . '" target="_blank">预览</a>' .
                        '&nbsp;&nbsp;<a href="' . $publish_url . '" target="_blank">发布</a>',
                        $article['article_id'],
                        $article['article_title'],
                        $article['article_category_name'],
                        $article['article_summary'],
                        $article['useFlg'] == 1 ? '启用' : '禁用'
                    )
                );
            }
        }

        echo json_encode($result_array);

    }
    
    /**
	 * 保存或修改文章
	 *
	 */
	private function saveArticle (){
		global $G_PGLOB;
		
		//实例化文章类
		$contentHandler = new contentHandler($G_PGLOB->DBCA);
        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
		$result_array = $contentHandler -> saveArticleInfo($_REQUEST);
		
		echo json_encode($result_array);
	}

    //查询文章评论信息
    function articleCommentList(){

        global $G_PGLOB;

        //实例化内容类
        $contentHandler = new contentHandler( $G_PGLOB->DBCA );

        //查询文章分类
        $comment_array = $contentHandler->searchArticleComments( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($comment_array) {
            foreach($comment_array as &$info) {
                $result_array['rows'][] = array(
                    'id' => $info['uuid'],
                    'cell' => array(
                        '<a href="javascript: void(0);" onclick="articleComment.reply(\'' . $info['uuid'] . '\')">回复</a>',
                        $info['article_title'],
                        $info['commentInfo'],
                        $info['createTime'],
                        $info['creator'],
                        $info['answerDateTime'],
                        $info['answerUserID'],
                        $info['commentStatus'] == 0? '未回复' : '已回复'
                    )
                );
            }
        }

        echo json_encode($result_array);

    }

    //保存文章回复内容
    function articleReply(){

        global $G_PGLOB;

        //实例化文章类
        $contentHandler = new contentHandler($G_PGLOB->DBCA);

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $result_array = $contentHandler -> articleReply($_REQUEST);

        echo json_encode($result_array);

    }

    //删除文章评论
    function delArticleComment(){

        global $G_PGLOB;

        //实例化文章类
        $contentHandler = new contentHandler($G_PGLOB->DBCA);

        //接收参数
        $delArr = $_REQUEST['delArr'];

        if( empty($delArr) ){
           echo json_encode( array( 'code'=>400 , 'message'=>'请选择要删除的评论' ) );
           return;
        }

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $result_array = $contentHandler -> delArticleComment( $delArr );

        echo json_encode($result_array);

    }

}